﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TrainingsApp.Model;
using TrainingsApp.Logic;

namespace TrainingsApp.IPADApp.UserControls2 {
    public partial class UC_Preview_Left : ControlBase {

        List<TS_TeachMaterials_Chapter_Entity> list = null;
        protected void Page_Load(object sender, EventArgs e) {
            if (!IsPostBack) {
                MenuBind();
            }
        }

        public void MenuBind() {
            if (this.CurPage.CurrentUser.UserType == UserType.Teacher) {
                list = TS_TeachMaterials_Chapter_Logic.TS_TeachMaterials_Chapter_ListByTeacherId(this.CurPage.CurrentUser.UserID);
            }
            else if (this.CurPage.CurrentUser.UserType == UserType.Children) {
                list = TS_TeachMaterials_Chapter_Logic.TS_TeachMaterials_Chapter_ListByStudentId(this.CurPage.CurrentUser.UserID);
            }
            rpL1.DataSource = list.Where(a => a.ParentIdx == 0).ToList();
            rpL1.DataBind();
        }

        protected void rpL1_ItemDataBound(object sender, RepeaterItemEventArgs e) {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
                Repeater rp = e.Item.FindControl("rpL2") as Repeater;
                TS_TeachMaterials_Chapter_Entity entity = (TS_TeachMaterials_Chapter_Entity)e.Item.DataItem;
                rp.DataSource = list.Where(a => a.ParentIdx == entity.Idx).ToList();
                rp.DataBind();
            }
        }

        protected void rpL2_ItemDataBound(object sender, RepeaterItemEventArgs e) {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
                Repeater rp = e.Item.FindControl("rpL3") as Repeater;
                TS_TeachMaterials_Chapter_Entity entity = (TS_TeachMaterials_Chapter_Entity)e.Item.DataItem;
                rp.DataSource = list.Where(a => a.ParentIdx == entity.Idx).ToList();
                rp.DataBind();
            }
        }

        protected void rpL3_ItemDataBound(object sender, RepeaterItemEventArgs e) {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
                Repeater rp = e.Item.FindControl("rpL4") as Repeater;
                TS_TeachMaterials_Chapter_Entity entity = (TS_TeachMaterials_Chapter_Entity)e.Item.DataItem;
                rp.DataSource = list.Where(a => a.ParentIdx == entity.Idx).ToList();
                rp.DataBind();
            }
        }
    }
}